package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Api(tags = "c端-訂單接口")
@Slf4j
@RestController("userOrderController")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 提交訂單
     * @param ordersSubmitDTO
     * @return
     */
    @ApiOperation("用戶下單")
    @PostMapping("/submit")
    @Transactional(rollbackFor = Exception.class)
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用戶下單：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);

        return Result.success(orderSubmitVO);
    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        log.info("生成预支付交易单：{}");
        return Result.success();
    }


    /**
     * 歷史訂單分頁查詢
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/historyOrders")
    public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("歷史訂單查詢：{}, {}", ordersPageQueryDTO);
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 查看訂單詳情
     * @param id
     * @return
     */
    @ApiOperation("查看訂單詳情接口")
    @GetMapping("/orderDetail/{id}")
    public Result detailsShow(@PathVariable("id") Long id) {
        log.info("查看訂單詳情，訂單id：{}", id);
        OrderVO orderVO = orderService.getDetails(id);
        return Result.success(orderVO);
    }


    /**
     * 取消訂單
     * @param id
     * @return
     */
    @ApiOperation("取消訂單接口")
    @PutMapping("/cancel/{id}")
    public Result Cancel(@PathVariable("id") Long id) throws Exception {
        log.info("取消訂單，被取消訂單id：{}", id);
        orderService.cancelById(id);
        return Result.success();
    }


    /**
     * 再來一單
     * @param id
     * @return
     */
    @ApiOperation("再來一單")
    @PostMapping("/repetition/{id}")
    public Result oneMoreOrder(@PathVariable("id") Long id){
        log.info("再來一單，原訂單id：{}", id);
        orderService.addAgain(id);
        return Result.success();
    }

    /**
     * 催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id) {
        log.info("用户催单：{}", id);
        orderService.reminder(id);

        return Result.success();
    }
}
